/*******************************************************************************
 * File name:     power_fail.c
 * Description: 
 * Project:       cc
 * Target:        LPC2478
 * Compiler:      arm-none-eabi-gcc
 * Date:          13-03-2012
 * Author:        Kuba
 * Based on:      ---
 ******************************************************************************/

/*==============================================================================
 Includes
==============================================================================*/
# include "LPC24xx.h"
# include "hdr_scb.h"
# include "typedef.h"
# include "irq.h"

# include "power_fail.h"

/*==============================================================================
 Defines
==============================================================================*/

/*==============================================================================
 Types
==============================================================================*/

/*==============================================================================
 Globals
==============================================================================*/
static void (*privateService)(void);

/*==============================================================================
 Static ISR prototypes
==============================================================================*/
static void pwrFailIsr(void) __attribute__ ((interrupt("IRQ")));

/*==============================================================================
 Static function prototypes
==============================================================================*/
static void dummyService (void);

/*==============================================================================
 Global function definitions
==============================================================================*/
/*------------------------------------------------------------------------------
 function name:   pwrFailInit
 description:     init power failure interrupt
 parameters:      service function, interrupt priority
 returned value:  true or false
------------------------------------------------------------------------------*/
bool pwrFailInit (void (*service)(void), enum priority priority)
{
   /* user service init */
   if (service == 0)
   {
      privateService = dummyService;   /* init with dummyService in case of 0 */
   }
   else
   {
      privateService = service;        /* init with user service */
   }
   /* hardware init */
   PINSEL4 |= PINSEL4_EINT2;                    /* set P2[12] as EINT2 */
   EXTMODE |= EXTMODE_EXTMODE2_EDGE;            /* edge sensitivity selected */
   EXTPOLAR |= EXTPOLAR_EXTPOLAR2_FALLING;
   /* VIC interrupt init */
   return (irqInit(irqEINT2, pwrFailIsr, priority));
}

/*==============================================================================
 Static function definitions
==============================================================================*/
/*------------------------------------------------------------------------------
 function name:   dummyService
 description:     for pointer init- does nothing
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
static void dummyService (void)
{
   return;
}

/*==============================================================================
 Static ISR definitions
==============================================================================*/
/*------------------------------------------------------------------------------
 function name:   pwrFailIsr
 description:     power failure interrupt service routine
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
static void pwrFailIsr(void)
{
   EXTINT |= EXTINT_EINT2;     /* reset the interrupt source */
   (*privateService)();       /* call user service */
   VICVectAddr = 0;
}
/******************************************************************************
* END OF FILE
******************************************************************************/



